package cn.zd.erp.auth.resource.service.ebo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.zd.erp.auth.resource.dao.dao.ResourceDao;
import cn.zd.erp.auth.resource.service.ebi.ResourceEbi;
import cn.zd.erp.auth.resource.vo.Tresource;
import cn.zd.erp.shiro.SessionInfo;
import cn.zd.erp.util.base.PageFilter;
import cn.zd.erp.util.base.PageResults;
import cn.zd.erp.util.base.Resource;
import cn.zd.erp.util.base.Tree;

@Transactional
@Service
public class ResourceEbo implements ResourceEbi {
	@Autowired
	private ResourceDao resourceDao;

	public void save(Tresource t) {
		resourceDao.saveOrUpdate(t);
	}

	public void update(Tresource t) {

	}

	public void delete(Tresource t) {
		resourceDao.delete(t);
	}

	public List<Tresource> getAll() {
		return null;
	}

	public Tresource get(Serializable uuid) {
		return resourceDao.get(uuid);
	}

	public Tresource getByName(String name) {
		String hql = "from Tresource t where t.name=?";
		Tresource t = resourceDao.getByHQL(hql, new String[] { name });
		return t;
	}

	public List<Tresource> dataGrid(Tresource t, PageFilter ph) {
		return null;
	}

	public PageResults<Resource> dataGridCount(Tresource t, int page,
			String sort, int rows, String order) {
		return null;
	}

	/**
	 * 获取资源树
	 */
	public List<Resource> datatree() {
		List<Resource> lr = new ArrayList<Resource>();
		String hql = "select distinct t from Tresource t left join fetch t.resource  order by t.seq";
		List<Tresource> l = resourceDao.getListByHQL(hql, null);
		if ((l != null) && (l.size() > 0)) {
			for (Tresource t : l) {
				Resource r = new Resource();
				BeanUtils.copyProperties(t, r);
				r.setCstate(t.getState());
				if (t.getResource() != null) {
					r.setPid(t.getResource().getId());
				}
				r.setIconCls(t.getIcon());
				lr.add(r);
			}
		}
		return lr;
	}

	@Override
	public List<Tree> getTree(SessionInfo sessionInfo) {
		List<Tresource> l = null;
		List<Tree> lt = new ArrayList<Tree>();

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("resourcetype", 0);// 菜单类型的资源
		System.out.println(sessionInfo.getUsertype().equals("0")+" stata"+sessionInfo.getUsertype());
		if (sessionInfo.getUsertype().toString().equals("0")) {
			String hql = "select distinct t from Tresource t  where t.resourcetype = 0  order by t.seq";
			l = resourceDao.getListByHQL(hql, null);
		} else {
			System.out.println("运行查找用户的");
			 Object[] values = {0,Long.valueOf(sessionInfo.getId())}; 
			params.put("userId", Long.valueOf(sessionInfo.getId()));// 自查自己有权限的资源
			String hql = "select distinct t from Tresource t join fetch t.roles role join role.users user where t.resourcetype = ? and user.id = ? order by t.seq";
			l = resourceDao.getListByHQL(hql, values);
		}
		if ((l != null) && (l.size() > 0)) {
			for (Tresource r : l) {
				Tree tree = new Tree();
				tree.setId(r.getId().toString());
				if (r.getResource() != null) {
					tree.setPid(r.getResource().getId().toString());
				} else {
					tree.setState("closed");
				}
				tree.setText(r.getName());
				tree.setIconCls(r.getIcon());
				Map<String, Object> attr = new HashMap<String, Object>();
				attr.put("url", r.getUrl());
				tree.setAttributes(attr);
				lt.add(tree);
			}
		}
		return lt;
	}

	@Override
	public List<Tree> listAllTree(boolean flag) {
		List<Tresource> l = null;
		List<Tree> lt = new ArrayList<Tree>();
		String hql;
		if (flag) {
			hql = "select distinct t from Tresource t left join fetch t.resource  order by t.seq";
			l = resourceDao.getListByHQL(hql, null);
		} else {
			hql = "select distinct t from Tresource t left join fetch t.resource where t.resourcetype =0 order by t.seq";
			l = resourceDao.getListByHQL(hql, null);
		}
		if ((l != null) && (l.size() > 0)) {
			for (Tresource r : l) {
				Tree tree = new Tree();
				tree.setId(r.getId().toString());
				if (r.getResource() != null) {
					tree.setPid(r.getResource().getId().toString());
				}
				tree.setText(r.getName());
				tree.setIconCls(r.getIcon());
				Map<String, Object> attr = new HashMap<String, Object>();
				attr.put("url", r.getUrl());
				tree.setAttributes(attr);
				lt.add(tree);
			}
		}
		return lt;
	}

	/**
	 * 通过角色id获取资源
	 */
	public Set<Tresource> getRoleRes(Long id) {
		// 查询中间表获得re
		return null;
	}

	public List<Tree> getTree() {
		return null;
	}

}
